import java.awt.*;
import java.util.ListIterator;

class Solution {
    public static void main(String[] args) {
        ListNode l1 = new ListNode(1) ;
        ListNode l2 = new ListNode(4) ;
        ListNode l3 = new ListNode(5) ;
        l1.next = l2 ;
        l2.next =l3 ;

        ListNode r1 = new ListNode(1) ;
        ListNode r2 = new ListNode(3) ;
        ListNode r3 = new ListNode(4) ;
        r1.next = r2 ;
        r2.next = r3 ;


        ListNode h2 = new ListNode(2) ;
        ListNode h3 = new ListNode(6) ;

        h2.next = h3 ;

        ListNode[] lists = {l1,r1,h2};






    }
    public static ListNode mergeKLists(ListNode[] lists) {
           if(lists==null) return null ;
       ListNode newHead = new ListNode() ;
       ListNode newEnd = newHead ;
       
       while(true){
           int n = 0 ;
           int count = 0 ;
          for(int i = 0 ; i < lists.length ; i++){
             if(lists[n]!=null && lists[i]!=null){
               if(lists[n].val >= lists[i].val){
                 n = i ;
             }
             }else if(lists[n] == null ){
                 count ++ ;
                 n ++ ;
             }
          }
          if(count == lists.length){
             return newHead.next ;
             }
          if(lists[n]!=null){
        newEnd.next = lists[n] ;
         lists[n] = lists[n].next ;
         newEnd = newEnd.next ;
          }
       }





       
       




    }
}